package adfc2.core;

/**
 * Глобальная переменная
 */
public final class GlobalVar<T> {
    private int index = -1;
    public final String name;
    public final T defval;

    public GlobalVar(String name, T defval) {
        this.name = name;
        this.defval = defval;
    }

    public int idx() {
        if (index == -1) {
            index = World.world().registerGlobalVar(name, defval);
        }
        return index;
    }

    @SuppressWarnings({"unchecked"})
    public T get() {
        return (T) World.world().global_vars.get(idx());
    }

    public void set(T val) {
        World.world().global_vars.set(idx(), val);
    }
}
